package com.apobates.forum.core.impl.event.listener;

import com.apobates.forum.core.dao.TopicActionCollectionDao;
import com.apobates.forum.core.entity.Topic;
import com.apobates.forum.core.entity.TopicActionCollection;
import com.apobates.forum.core.impl.event.PlugTopicPublishEvent;
import com.github.davidmarquis.redisq.Message;
import com.github.davidmarquis.redisq.consumer.MessageListener;
import com.github.davidmarquis.redisq.consumer.retry.RetryableMessageException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 插件话题发布事件侦听器.创建相关的动作记录
 * @author xiaofanku
 * @since 20200530
 */
@Component("plugTopicActionEntry")
public class PlugTopicRelateActionListener implements MessageListener<PlugTopicPublishEvent> {
    @Autowired
    private TopicActionCollectionDao topicActionCollectionDao;
    private final static Logger logger = LoggerFactory.getLogger(PlugTopicRelateActionListener.class);
    
    @Override
    public void onMessage(Message<PlugTopicPublishEvent> e) throws RetryableMessageException {
        logger.info("[PlugTopic][PublishEvent][2]插件话题关联动作开始执行");
        PlugTopicPublishEvent tpe = e.getPayload();
        Topic tr = tpe.getPlugTopic();
        long contentPostsId = tpe.getPlugContent();
        //如何判断是否重复?
        TopicActionCollection tac = new TopicActionCollection(
                tpe.getNickname(),
                tpe.getMember(),
                tpe.getAction(),
                tr.getTitle(),
                tr.getId(),
                contentPostsId,
                tpe.getIpAddr(),
                tpe.getToken(),
                tpe.getMember());
        topicActionCollectionDao.save(tac);
        logger.info("[PlugTopic][PublishEvent][2]插件话题关联动作执行结束");
    }
}